﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CalemShop.Classes;
using System.Threading;

namespace CalemShop.Forms
{
    public partial class FrmSearchItem : FrmBaseDock
    {
        private delegate void UpdateSearchProgressHandler(string message, bool isDone);

        private delegate void UpdateSearchResultHandler(DataTable result);

        public FrmSearchItem()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            dataGridView.AutoGenerateColumns = false;
        }

        public FrmSearchItem(FrmCalemShop shop)
            : base(shop)
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            dataGridView.AutoGenerateColumns = false;
        }


        private void UpdateSearchingProgress(string message, bool isDone)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new UpdateSearchProgressHandler(UpdateSearchingProgress), message, isDone);
            }
            else
            {
                try
                {
                    if (isDone)
                    {
                        lbStatus.Text = "Done";
                        progressBar.Value = 20;
                        progressBar.Visible = false;
                    }
                    else
                    {
                        lbStatus.Text = message;
                        progressBar.Value = 20;
                        progressBar.Visible = true;
                    }
                }
                catch (Exception) { }
            }
        }


        private void UpdateSearchResult(DataTable result)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new UpdateSearchResultHandler(UpdateSearchResult), result);
            }
            else
            {
                this.dataGridView.DataSource = result;
            }
        }

        private void DoSearch()
        {
            try
            {
                if (string.IsNullOrEmpty(txtSearch.Text))
                {
                    return;
                }

                int autoKey = 0;
                int.TryParse(txtSearch.Text, out autoKey);
                DataTable searchData = null;
                if (autoKey > 0)
                {
                    this.UpdateSearchingProgress("Đang tìm kiếm ...", false);
                    searchData = Database.searchItemInStockByAutoKey(txtSearch.Text.Trim());
                    UpdateSearchResult(searchData);
                    
                    return;
                }

                this.UpdateSearchingProgress("Đang tìm kiếm ...", false);
                searchData = Database.searchItemInStock(txtSearch.Text.Trim().ToUpper());
                if (searchData == null || searchData.Rows.Count == 0)
                {
                    searchData = Database.searchItemInStockByName(txtSearch.Text.Trim());
                }

                
                UpdateSearchResult(searchData);

            }
            catch (Exception e)
            {
                Reporter.WriteErrorEx(e);
            }
        }

        private void backgroundSearch_DoWork(object sender, DoWorkEventArgs e)
        {
            DoSearch();
        }

        private void backgroundSearch_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.UpdateSearchingProgress(string.Empty, true);
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    e.Handled = true;
                    this.Enabled = false;
                    if (backgroundSearch.IsBusy)
                    {
                        return;
                    }
                    this.backgroundSearch.RunWorkerAsync();
                }
                finally
                {
                    if (this.Enabled == false)
                    {
                        this.Enabled = true;
                    }
                }
            }
            else if (e.KeyCode == Keys.Down)
            {
                e.SuppressKeyPress = false;
                dataGridView.Focus();
            }
        }

        private void chShowCost_CheckedChanged(object sender, EventArgs e)
        {
            if (chShowCost.Checked)
            {
                FrmLogin frmLogin = new FrmLogin();
                if (frmLogin.ShowDialog(this) == DialogResult.OK && frmLogin.loginOk && frmLogin.isAdmin)
                {
                    clCost.Visible = chShowCost.Checked;
                }
                else
                {
                    MessageBox.Show(this, "Cần phải đăng nhập mới sử dụng được tính năng này.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                clCost.Visible = false;
            }
        }
    }
}
